/**
 * Created by camiez on 2017/4/23.
 */
var express = require('express');
var iconv = require('iconv-lite')
var app = express();
var request = require('request');
var cheerio = require('cheerio');
const fs = require('fs');
const db = require('./mysql');

app.get('/', function(req, res){
    request('http://www.wanxianshan.net/212339.shtml',null, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            //返回的body为抓到的网页的html内容
            var html = iconv.decode(body, 'gb2312');
            var $ = cheerio.load(html,{decodeEntities: false}); //当前的$符相当于拿到了所有的body里面的选择器
            // var novalText=$('.main-text-wrap').html(); //拿到小说的内容
            var catgory = $('.nav>span').text().split(/\d+/);
            console.log(catgory);
            // for (var i = 0; i < catgory.length; i++) {
                db.query('update book set chapter = ? where bookId = 1019',[catgory],function (resulr) {
                    res.sned('添加成功')
                })
            // }
            // var writerStream = fs.createWriteStream('./public/text/flhh3.txt');
            // writerStream.write(novalText,'UTF8');
            // 标记文件末尾
            //writerStream.end();

            // 处理流事件 --> data, end, and error
            //writerStream.on('finish', function() {
            //     console.log("写入完成。");
            //});

            //writerStream.on('error', function(err){
            //     console.log(err.stack);
            //});

            res.send("程序执行完毕");
        }
    })
});
app.listen(3000,function (req,res,next) {
    console.log('秦董')
});